package com.gdue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdue.commons.R;
import com.gdue.dto.SetmealDto;
import com.gdue.entity.Category;
import com.gdue.entity.Setmeal;
import com.gdue.exception.CustomException;
import com.gdue.service.CategoryService;
import com.gdue.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService  setmealService;
    
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page>  pageR(int page,int pageSize,String name){
        Page<Setmeal>   pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto>    pageDto=new Page<>();

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lambdaQueryWrapper.orderByAsc(Setmeal::getPrice);
        setmealService.page(pageInfo,lambdaQueryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<Setmeal> records=pageInfo.getRecords();

        List<SetmealDto>    list=records.stream().map((item)->{
            SetmealDto  setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            //分类id
            Long categoryId=item.getCategoryId();
            //根据id查询分类
            Category categroy = categoryService.getById(categoryId);
            if (categroy!=null){
                String cagetoryName = categroy.getName();
                setmealDto.setCategoryName(cagetoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        pageDto.setRecords(list);

        return R.success(pageDto);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto    setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 修改套餐前根据id查询套餐相关信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long   id){
        SetmealDto setmealDto = setmealService.getWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String>  update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * (批量)删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String>  delete(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 修改套餐的销售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatusById(@PathVariable int status,@RequestParam List<Long> ids){
        setmealService.updateStatusById(status,ids);
        return R.success("状态修改成功");
    }
}
